package api

import (
	"server/global"
	operatemodel "server/model/operate"
	"server/myutils"

	"github.com/gin-gonic/gin"
)

//进行数据绑定的时候可以进行数据验证,验证不过会报错
type log struct {
	Username string `json:"username" binding:"required"`
	Password string `json:"password" binding:"required" `
}

//@Summary 登录
//@Router /login [post]
//@Description 登录接口
//@Tags Posts
//@Accept json
//@Produce json
//@Param data  body log  true "传入用户名和密码"
//@Success 200 {object} model.StandardResponse{} "接口被正确调用,但是调用结果看status状态码"
func Login(req *gin.Context) {
	var data = new(log)
	if err := req.ShouldBindJSON(data); err != nil {
		myutils.ResultJSON(req, global.ERROR_FORMAT, nil)
		return
	}
	code, token, role := operatemodel.LoginAdmin(data.Username, data.Password)
	if code != global.SUCCESS {
		myutils.ResultJSON(req, code, nil)
		return
	}
	myutils.ResultJSON(req, code, map[string]interface{}{"token": "Bearer " + token, "role": role})
}
